package com.bdqn.service;

import com.bdqn.mapper.SysRoleMapper;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.taglibs.standard.tlv.JstlSqlTLV;

import java.util.ArrayList;
import java.util.List;

/**
 * SysRoleServiceImpl
 *
 * @author LILIBO
 * @since 2022-09-13
 */
public class SysRoleServiceImpl implements SysRoleService {

    /**
     * 日志
     */
    private static final Logger logger = LogManager.getLogger(SysUserServiceImpl.class);

    /**
     * 获取所有角色数据
     *
     * @return
     */
    @Override
    public List<SysRole> getAll() {
        List<SysRole> list = new ArrayList<>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(SysRoleMapper.class).getAll();
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public List<SysRole> queryPageByRealNameAndRoleId(int pageIndex, int pageSize) {
        List<SysRole> list = new ArrayList<>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(SysRoleMapper.class).queryPageByRealNameAndRoleId(pageIndex,pageSize);
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public int deleteRole(int id) {
        int row=0;
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            row=sqlSession.getMapper(SysRoleMapper.class).deleteRole(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return row;
    }

    @Override
    public int codeCount(String code) {
        int row=0;
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            row=sqlSession.getMapper(SysRoleMapper.class).codeCount(code);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return row;
    }

    @Override
    public int add(SysRole sysRole) {
        int row=0;
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            row=sqlSession.getMapper(SysRoleMapper.class).add(sysRole);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return row;
    }

    @Override
    public int count() {
        int row=0;
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            row=sqlSession.getMapper(SysRoleMapper.class).count();
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return row;
    }

    @Override
    public List<SysRole> getByRoleName() {
        List<SysRole> list = new ArrayList<>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(SysRoleMapper.class).getByRoleName();
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public SysRole getBySysRole(int id) {
        SysRole sysRole=null;
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            sysRole= sqlSession.getMapper(SysRoleMapper.class).getBySysRole(id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return sysRole;
    }

}
